package com.swt.draw.service.impl;

import com.swt.draw.dao.YjTemplateMapper;
import com.swt.draw.entity.YjTemplate;
import com.swt.draw.entity.Yjk;
import com.swt.draw.example.YjTemplateExample;
import com.swt.draw.service.TemplateService;
import com.swt.draw.vo.TemplateVO;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * @ClassName
 * @Description TODO
 * @Author Administrator
 * @Date 2019/10/27 0027 23:22
 * @Version 1.0
 **/
@Service("templateService")
public class TemplateServiceImpl implements TemplateService {

    @Resource
    private YjTemplateMapper yjTemplateMapper;

    @Override
    public List<TemplateVO> getTemplates(String jh, Date time, String userId) {
        return yjTemplateMapper.getTemplates(jh, time, userId);
    }

    @Override
    public List<YjTemplate> getYjTemplates(String userId, Integer start, Integer end) {
        return yjTemplateMapper.getYjTemplates(userId, start, end);
    }

    @Override
    public Integer getYjTemplateCount(String userId) {
        return yjTemplateMapper.getYjTemplateCount(userId);
    }

    @Override
    public List<YjTemplate> getYjTemplateByJhWithTime(String jh, String time){
        YjTemplateExample example = new YjTemplateExample();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ");
        Date date = null;
        try {
            date = format.parse(time.replace(" ", "+"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        YjTemplateExample.Criteria criteria = example.createCriteria();
        criteria.andJhEqualTo(jh);
        criteria.andSjrqEqualTo(date);
        List<YjTemplate> templates = yjTemplateMapper.selectByExample(example);
        return templates;
    }

    @Override
    public List<Yjk> getYjksFromTemplate(String jh, String time, String userId) {
        return yjTemplateMapper.getYjksFromTemplate(jh, time, userId);
    }

    @Override
    public Integer delTemplateByJhAndTime(String jh, String time) throws ParseException {
        YjTemplateExample example = new YjTemplateExample();
        YjTemplateExample.Criteria criteria = example.createCriteria();
        criteria.andJhEqualTo(jh);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ");
        criteria.andSjrqEqualTo(format.parse(time));
        return yjTemplateMapper.deleteByExample(example);
    }
}
